﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using AnimEditor.Anim;
using AnimEditor.Picture;
using AnimEditor.Define;
using AnimEditor.Tile;


namespace AnimEditor
{
	public class GridPanel : Panel
	{
		int mSelectedTile = -1;
		public int SelectedTile
		{
			get { return mSelectedTile; }
			set { mSelectedTile = value; }
		}

		public Bitmap ShowBitMap
		{
			get { return PicMgr.Instance.BitMap; }
			set
			{
				PicMgr.Instance.BitMap = value;
				if (PicMgr.Instance.BitMap != null)
				{
					this.AutoScrollMinSize = new Size(PicMgr.Instance.BitMap.Width, PicMgr.Instance.BitMap.Height);
				}
			}
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			int vscorllvalue = this.VerticalScroll.Value;
			int hscorllvalue = this.HorizontalScroll.Value;

			Bitmap bmp = new Bitmap(this.Width, this.Height);
			Graphics g = Graphics.FromImage(bmp);

			if (PicMgr.Instance.BitMap != null)
			{
				g.DrawImage(PicMgr.Instance.BitMap, 0, 0, new Rectangle(HorizontalScroll.Value, VerticalScroll.Value, Size.Width, Size.Height), GraphicsUnit.Pixel);
			}

			SolidBrush brush = new SolidBrush(Color.FromArgb(30, 100, 255, 0));
			Pen Tilepen = new Pen(Color.FromArgb(80, 255, 0));
			List<TileData> Tiles = TileMgr.Instance.Tile_Sprites;
			// 画选中瓦片图
			if (SelectedTile != -1)
			{
				brush.Color = Color.FromArgb(60, 100, 255, 0);
				Rectangle animRect = Tiles[SelectedTile].Rect;
				g.FillRectangle(brush, new Rectangle(animRect.X - hscorllvalue, animRect.Y - vscorllvalue, animRect.Width, animRect.Height));
			}
			// 画除了选中的剩余瓦片图
			brush.Color = Color.FromArgb(30, 100, 255, 0);
			for (int i = 0; i < Tiles.Count; ++i)
			{
				Rectangle animRect = Tiles[i].Rect;
				if (i != SelectedTile)
					g.FillRectangle(brush, new Rectangle(animRect.X - hscorllvalue, animRect.Y - vscorllvalue, animRect.Width, animRect.Height));
				g.DrawRectangle(Tilepen, new Rectangle(animRect.X - hscorllvalue, animRect.Y - vscorllvalue, animRect.Width, animRect.Height));
			}

			this.CreateGraphics().DrawImage(bmp, 0, 0);  
			base.OnPaint(e);
		}
	}
}
